package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;




/*import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;*/
import javax.swing.*;
import javax.swing.table.*;

import controller.AddcontactListener;
import controller.AddcontactmenuListener;
import controller.DeleteListener;
import controller.DetailsListener;
import controller.EmailListener;
import controller.HomepageListener;
import controller.ListmenuListener;
import controller.ModifyListener;
import model.Adressbook;
import model.Contact;

/**
 * 
 * @author Nelly-Kim TRAN
 * 
 * @param backgroundpanel
 * 					the panel on the background that contains 2 others panels
 * @param scrollpanel
 * 					panel on the left on the background. contains a scrollpane where we can see the list of contacts
 * @param infopanel
 * 					panel on the right on the background. contains a scollpane where we display the information of the contacts
 * @param scrollPane
 * 					contains the list of contacts
 * @param infoscrollpane
 * 					contaisn the information of one contact
 * 				
 *
 */

public class ListView extends JFrame {
	
	JPanel backgroundpanel=new JPanel(new GridLayout(1,2));
	JPanel scrollpanel = new JPanel();
	JPanel infopanel = new JPanel(new GridLayout(0,1));
	JScrollPane scrollPane;
	JScrollPane infoscrollpane;
	
	
	//Menu
	JMenuBar menubar=new JMenuBar();
	JMenu menu1=new JMenu("Add Contact");
	JMenuItem item1=new JMenuItem("Add!");
	JMenu menu4=new JMenu("List of Contacts");
	JMenuItem item4=new JMenuItem("See the list!");
	
	
    
    public JPanel getInfopanel() {
		return infopanel;
	}


	public void setInfopanel(JTextField label1, JTextField label2, JTextField label3, JTextField label4, JTextField label5, JTextField label6, JTextField label7,
			JTextField label8, JButton label9, JButton label10, JButton label11, JTextField label12, JTextField label13) {
		
		infopanel.add(new JLabel("Firstname"));
		infopanel.add(label1);
		label1.setEditable(false);
		infopanel.add(new JLabel("Lastname"));
		infopanel.add(label2);
		label2.setEditable(false);
		infopanel.add(new JLabel("Home address"));
		infopanel.add(label3);
		label3.setEditable(false);
		infopanel.add(new JLabel("Company address"));
		infopanel.add(label4);
		label4.setEditable(false);
		infopanel.add(new JLabel("Personal mobile"));
		infopanel.add(label5);
		label5.setEditable(false);
		infopanel.add(new JLabel("Personal home"));
		infopanel.add(label6);
		label6.setEditable(false);
		infopanel.add(new JLabel("Professionnal mobile"));
		infopanel.add(label7);
		label7.setEditable(false);
		infopanel.add(new JLabel("Professionnal office"));
		infopanel.add(label8);
		label8.setEditable(false);
		infopanel.add(new JLabel("Homepage"));
		infopanel.add(label9);
		infopanel.add(new JLabel("Personal email"));
		infopanel.add(label10);
		infopanel.add(new JLabel("Professionnal email"));
		infopanel.add(label11);
		infopanel.add(new JLabel("Photo"));
		infopanel.add(label12);
		label12.setEditable(false);
		infopanel.add(new JLabel("Group"));
		infopanel.add(label13);
		label13.setEditable(false);
		infopanel.add(new JLabel(" "));

		JButton modify= new JButton("Modify!");
		infopanel.add(modify);
		
		JButton delete= new JButton("Delete Contact!");
		infopanel.add(delete);
		this.validate();
		
		
		ModifyListener listener =new ModifyListener(label1, label2, label3, label4,label5, label6,label7, label8,
				label9, label10,label11, label12,label13, infopanel);
		modify.addActionListener(listener);
		
		DeleteListener listener2 =new DeleteListener(this, label1, label2);
		delete.addActionListener(listener2);
		
		HomepageListener listener3= new HomepageListener(label9);
		label9.addActionListener(listener3);
		
		EmailListener listener4= new EmailListener(label10);
		label10.addActionListener(listener4);
		
		EmailListener listener5= new EmailListener(label11);
		label11.addActionListener(listener5);
		
	}

	
	
	public ListView(){
		super();
		
		setTitle("My address book"); 
		setSize(600,600);
		
	
	
		//JPanel for the list of contacts
		scrollpanel.setPreferredSize(new Dimension(300,600));
		scrollpanel.setLayout(new GridLayout(0,1));
		
		
		//displays the list of contact on JButtons
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";
	
	try {
		 
		br = new BufferedReader(new FileReader("monfichier2.txt"));
		while ((line = br.readLine()) != null) {
 
		        // use comma as separator
			String[] contact = line.split(cvsSplitBy);
			JButton bouton=new JButton(contact[0]+" "+contact[1]);
			scrollpanel.add(bouton);
			
			DetailsListener listener1=new DetailsListener(bouton, infopanel, this);
			bouton.addActionListener(listener1);

		}	
	} catch (FileNotFoundException e1) {
		e1.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	
				
		
		setContentPane(scrollpanel);
		
		//JPanel for the information about the contacts
		infopanel.setPreferredSize(new Dimension(300,600));
		infopanel.setLayout(new GridLayout(0,1));
		setContentPane(infopanel);
		
		//the JScrollPane contains scrollpanel
		scrollPane=new JScrollPane(scrollpanel);
		
		infoscrollpane=new JScrollPane(infopanel);
		
		backgroundpanel.add(scrollPane);
		backgroundpanel.add(infoscrollpane);
		//setContentPane(backgroundpanel);
		
 		
		//Menu initialization
		menubar.add(menu1);	
		menu1.add(item1);
		menubar.add(menu4);
		menu4.add(item4);
		setJMenuBar(menubar);
		setContentPane(backgroundpanel);
		

		//Listener for the menu
				AddcontactmenuListener listener1=new AddcontactmenuListener(this);
				item1.addActionListener(listener1);
				
				ListmenuListener listener2=new ListmenuListener(this);
				item4.addActionListener(listener2);
								
	}

}
